$(function(){

    var allPage,page,status,searchKey,auditStatus = '';

    // 页面初始化....请求全部订单...默认显示申请退款
    page = getRefuseGoodsList(  '',1,10,4,1,'' );
    getPage(page);

    function getRefuseGoodsList(  auditStatus,page,size,status,type,searchKey )
    {

        $.ajax({
            async:false,
            cache:false,
            type:'post',
            headers: {"Accept-CMS-Token":token},
            url:refundorderlist,
            data:{
                auditStatus:auditStatus,
                page:page,
                size:size,
                status:status,
                type:type,
                querystr:searchKey
            },
            dataType:'json',
            success:successFn,
            error:function(){
                console.log('查询退货页面接口请求失败')
            },
        });

        return allPage;
    };

    //订单列表请求成功之后....要执行此函数....
    function successFn(data)
    {
        if( data.resultCode == 200 )
        {
            allPage = data.count;
            var value = data.value;
            // 清空表格
            clearTable();
            for( var i=0;i<value.length;i++ )
            {
                $('.refuse_goods').append('<tr orderId="'+value[i].id+'"></tr>');//属性

                $('.refuse_goods tr').eq(i+1).append('<td>'+(i+1)+'</td>');

                $('.refuse_goods tr').eq(i+1).append('<td>'+changeNull(value[i].refundNum)+'</td>');//退款编号

                $('.refuse_goods tr').eq(i+1).append('<td>'+changeNull(value[i].shopOrderNum)+'</td>');//订单编号

                $('.refuse_goods tr').eq(i+1).append('<td>'+changeNull(value[i].commodityName)+'</td>');//商品名称

                $('.refuse_goods tr').eq(i+1).append('<td>0</td>');//邮费

                $('.refuse_goods tr').eq(i+1).append('<td>'+changeNull(value[i].backMoney)+'</td>');//退款金额

                $('.refuse_goods tr').eq(i+1).append('<td>'+changeNull(value[i].userId)+'</td>');

                $('.refuse_goods tr').eq(i+1).append('<td>'+changeNull(value[i].userNickName)+'</td>');

                $('.refuse_goods tr').eq(i+1).append('<td>'+getMoth(value[i].buyTime)+'</td>');//下单时间

                $('.refuse_goods tr').eq(i+1).append('<td>'+getMoth(value[i].payTime)+'</td>');//支付时间.

                $('.refuse_goods tr').eq(i+1).append('<td>'+getMoth(value[i].createTime)+'</td>');//申请退款时间

                $('.refuse_goods tr').eq(i+1).append('<td>'+changeRefuseStatus(value[i].status,value[i].auditStatus)+'</td>');//订单状态

                $('.refuse_goods tr').eq(i+1).append('<td>'+changeServiceWord(value[i].backType)+'</td>');//服务类型

                $('.refuse_goods tr').eq(i+1).append('<td>'+changeNull(value[i].receiveName)+'</td>');//收货人

                $('.refuse_goods tr').eq(i+1).append('<td>'+changeNull(value[i].backExpressName)+'</td>');//退货快递公司

                $('.refuse_goods tr').eq(i+1).append('<td>'+changeNull(value[i].backExpressNum)+'</td>');//退货物流单号

                $('.refuse_goods tr').eq(i+1).append('<td>'+'<a class="look_order" href="#">查看</a>'+'</td>');//操作

            };
            // console.log(data);

        }
        else if( data.resultCode == 500 )
        {//这种状态下...要搜索的东西是空的...
            clearTable();
            $('.refuse_goods').append('<tr><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td>您搜索的内容不存在</td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td><td></td></tr>');
            hintUser();
            allPage = 1;
        }
        else if(data.resultCode == 515)
        {
            reLogin()
        };
        console.log('查询订单接口请求成功!!!!')
    };

    //清空表格当中的数据,并且追加表头
    function clearTable()
    {
        $('.refuse_goods').html('');
        $('.refuse_goods').append('<tr></tr>');
        $('.refuse_goods tr').eq(0).append('<th>序号</th><th>退款编号</th><th>订单编号</th><th>商品名称</th><th>邮费</th><th>退款金额</th><th>买家ID</th><th>买家昵称</th><th>下单时间</th><th>支付时间</th><th>申请退货时间</th><th>订单状态</th><th>服务类型</th><th>收货人</th><th>退货快递公司</th><th>退货物流单号</th><th>操作</th>');
    };

    // 分页插件封装   为分页插件传递数据
    function getPage(allPage)
    {
        $('.M-box2').pagination({
            pageCount:allPage,
            jump:true,
            coping:true,
            homePage:'首页',
            endPage:'末页',
            prevContent:'上页',
            nextContent:'下页',
            callback:function (api){//当点击页面当中的时候,会出发这个callback函数;;;;
                page = api.getCurrent()+"";//得到的是要跳转到的那一页的页码;
                status = $('.refuse_status option:selected').val();
                searchKey = $('input[name = refuse_cont]').val().sTrim();
                if( status == 2 )
                {
                    if( $('.refuse_status option:selected').html() == '退货完成' )
                    {
                        auditStatus = 2 ;
                    }
                    else if( $('.refuse_status option:selected').html() == '申请拒绝' )
                    {
                        auditStatus = 1 ;
                    };

                };
                getRefuseGoodsList(  auditStatus,page,10,status,1,searchKey )
            }
        });
    };

    // 页面的搜索功能....需要后台配合..
    $('.search_refuse').click(function(){

        status = $('.refuse_status option:selected').val();
        searchKey = $('input[name = refuse_cont]').val().sTrim();
        if( status == 2 )
        {
            if( $('.refuse_status option:selected').html() == '退货完成' )
            {
                auditStatus = 2 ;
            }
            else if( $('.refuse_status option:selected').html() == '申请拒绝' )
            {
                auditStatus = 1 ;
            };

        };
        page = getRefuseGoodsList(  auditStatus,1,10,status,1,searchKey );
        getPage(page);
        exportOrder(auditStatus,status,1,searchKey);//导出订单
    });

    //当输入文本框中时,,,按下enter键,,,发起搜索,,,
    $(document).keydown(function(event){
        var ev = ev || event;

        if( ev.keyCode == 13 )
        {
            status = $('.refuse_status option:selected').val();
            searchKey = $('input[name = refuse_cont]').val().sTrim();
            if( status == 2 )
            {
                if( $('.refuse_status option:selected').html() == '退货完成' )
                {
                    auditStatus = 2 ;
                }
                else if( $('.refuse_status option:selected').html() == '申请拒绝' )
                {
                    auditStatus = 1 ;
                };

            };
            page = getRefuseGoodsList(  auditStatus,1,10,status,1,searchKey );
            getPage(page);
            exportOrder(auditStatus,status,1,searchKey);//导出订单
        };

    });

    // 改变下拉菜单...页面当中的列表内容发生变化...
    function changeXiaLa(x)
    {
        $(x).change(function (){
            status = $('.refuse_status option:selected').val();
            searchKey = $('input[name = refuse_cont]').val().sTrim();
            if( status == 2 )
            {
                if( $('.refuse_status option:selected').html() == '退货完成' )
                {
                    auditStatus = 2 ;
                }
                else if( $('.refuse_status option:selected').html() == '申请拒绝' )
                {
                    auditStatus = 1 ;
                };

            };
            page = getRefuseGoodsList(  auditStatus,1,10,status,1,searchKey );
            getPage(page);
            exportOrder(auditStatus,status,1,searchKey);//导出订单
        });
    };
    changeXiaLa('.refuse_status');

    // 点击查看...跳转到查看订单的详情页
    $(document).on('click','.look_order',function(){

        var orderId = $(this).parent('td').parent('tr').attr('orderid');
        window.location.href = look_refuseGoods  + '?orderId='+orderId;

    });

    //从后台获取所有订单的数据...为导出 表格做准备....导出全部订单...
    function exportOrder(auditStatus,status,type,searchKey)
    {
        //清空隐藏的表格的内容...并且追加表头...
        clearHideRefuseTable('.goods_table');

        $.ajax({
            async:false,
            type:'post',
            headers: {"Accept-CMS-Token":token},
            url:exportrefund,
            data:{
                auditStatus:auditStatus,
                status:status,
                type:type,
                querystr:searchKey
            },
            dataType:'json',
            success:function(data){
                if(data.resultCode == 200)
                {
                    var list = data.value;
                    var len = list.length;
                    for( var i=0;i<len;i++ )
                    {
                        $('.goods_table tbody').append('<tr orderid="'+list[i].id+'"></tr>');

                        $('.goods_table tbody tr').eq(i).append('<td>'+(i+1)+'</td>');//序号

                        $('.goods_table tbody tr').eq(i).append('<td>'+changeNull(list[i].shopRefundRecord.refundNum)+'</td>');//退款编号

                        $('.goods_table tbody tr').eq(i).append('<td>'+changeNull(list[i].shopOrder.orderNum)+'</td>');//订单编号

                        $('.goods_table tbody tr').eq(i).append('<td>'+changeNull(list[i].shopRefundRecord.commodityName)+'</td>');//商品名称

                        $('.goods_table tbody tr').eq(i).append('<td>'+changeNull(list[i].shopRefundRecord.amount)+'</td>');//数量
                        $('.goods_table tbody tr').eq(i).append('<td>'+changeNull(list[i].shopRefundRecord.colors)+'</td>');//颜色

                        $('.goods_table tbody tr').eq(i).append('<td>'+changeNull(list[i].shopRefundRecord.sizeName)+'</td>');//尺寸
                        $('.goods_table tbody tr').eq(i).append('<td>'+changeNull(list[i].shopRefundRecord.modelName)+'</td>');//型号

                        $('.goods_table tbody tr').eq(i).append('<td>'+changeNull(list[i].shopRefundRecord.prices)+'</td>');//单价
                        $('.goods_table tbody tr').eq(i).append('<td>0</td>');//邮费

                        $('.goods_table tbody tr').eq(i).append('<td>'+changeNull(list[i].shopRefundRecord.backMoney)+'</td>');//金额

                        $('.goods_table tbody tr').eq(i).append('<td>'+changeNull(list[i].shopRefundRecord.userId)+'</td>');//买家ID

                        $('.goods_table tbody tr').eq(i).append('<td>'+changeNull(list[i].shopRefundRecord.userNickName)+'</td>');//买家昵称

                        $('.goods_table tbody tr').eq(i).append('<td>'+getMoth(list[i].shopOrder.createTime)+'</td>');//下单时间

                        $('.goods_table tbody tr').eq(i).append('<td>'+getMoth(list[i].shopOrder.payTime)+'</td>');//支付时间

                        $('.goods_table tbody tr').eq(i).append('<td>'+getMoth(list[i].shopOrder.expressSendTime)+'</td>');//发货时间

                        $('.goods_table tbody tr').eq(i).append('<td>'+changeServiceWord(list[i].shopRefundRecord.backType)+'</td>');//服务类型
                        $('.goods_table tbody tr').eq(i).append('<td>'+getMoth(list[i].shopRefundRecord.createTime)+'</td>');//申请退款时间

                        $('.goods_table tbody tr').eq(i).append('<td>'+changeRefuseStatus(list[i].shopRefundRecord.status,list[i].shopRefundRecord.auditStatus)+'</td>');//订单状态

                        $('.goods_table tbody tr').eq(i).append('<td>'+changeNull(list[i].shopRefundRecord.backExpressName)+'</td>');//退货快递公司

                        $('.goods_table tbody tr').eq(i).append('<td style="vnd.ms-excel.numberformat:@;">'+changeNull(list[i].shopRefundRecord.backExpressNum)+'</td>');//退货物流单号

                    };
                    // console.log(data)
                }
                else if( data.resultCode == 500 )
                {//这种情况下没有数据
                    $('.goods_table tbody').append('<tr><td></td><td></td></td><td>亲,没有数据...</td></tr>');
                }
                else if(data.resultCode == 515)
                {
                    reLogin();
                };

                // console.log($('.all_table tbody tr').length);
                console.log('导出退款订单接口请求成功!!!')
            },
            error:function(){
                console.log('导出退款订单接口请求失败')
            },
        });
    };
    exportOrder('',4,1,'');//页面初始化...默认导出全部订单....





});
